/*----------------------------------------------------------------------------*/
/* Copyright 2011 http://www.kmftech.com                                      */
/*                                                                            */
/* This file is part of kmftech.                                              */
/*                                                                            */
/* kmftech is free software: you can redistribute it and/or modify it under   */
/* the terms of the GNU General Public License as published by the Free       */
/* Software Foundation, version 2 of the License, or (at your option) any     */
/* later version.                                                             */
/*                                                                            */
/* kmftech is distributed in the hope that it will be useful, but WITHOUT ANY */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  */
/* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more      */
/* details.                                                                   */
/*                                                                            */
/* You should have received a copy of the GNU General Public License along    */
/* with kmftech. If not, see http://www.gnu.org/licenses/.                    */
/*----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------*/
/*---------------------------- DOCUMENT HISTORY ------------------------------*/
/* Ver: 1.0    Author: Karim Mansour                      Date: 30 April 2011 */
/* Ver: X.X    Author:                                    Date:               */
/*----------------------------------------------------------------------------*/
/*---------------------    DOCUMENT CHANGE HISTORY ---------------------------*/
/* Ver: 1.0    Change: Initial Version.                                       */
/* Ver: X.X    Change:                                                        */
/*----------------------------------------------------------------------------*/


/*                           Needed Header Files                              */
#include <avr/interrupt.h>
#include <inttypes.h>
#include "kmftech_types.h"

/*                           Functions Prototypes                             */
void vd_bsc_timer_set_clk_div(uint8 u8_timer_type,
                              uint8 u8_timer_nb,
                              uint16 u16_div_factor);
void vd_bsc_timer_set_val(uint8 u8_timer_type,
                          uint8 u8_timer_nb,
                          uint16 u16_value);
void vd_bsc_timer_int_ovf_enable(uint8 u8_timer_type,
                                 uint8 u8_timer_nb);

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_timer_set_clk_div                                      */
/* Description: Divide the count frequency (system frequency) of a certain    */
/*              timer                                                         */
/* Input(s)   : Input  1: uint8 - Counter type (8,16 bits)                    */
/*            : Input  2: uint8 - Counter number                              */
/*            : Input  3: uint16 - Division factor                            */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_timer_set_clk_div(uint8 u8_timer_type,
                              uint8 u8_timer_nb,
                              uint16 u16_div_factor)
{
   uint8 u8_div_factor_code;
   switch (u16_div_factor)
   {
      case 1:
         u8_div_factor_code = 0x01;
         break;
      case 8:
         u8_div_factor_code = 0x02;
         break;
      case 64:
         u8_div_factor_code = 0x03;
         break;
      case 256:
         u8_div_factor_code = 0x04;
         break;
      case 1024:
         u8_div_factor_code = 0x05;
         break;
      default:
         u8_div_factor_code = 0x01;
         break;
   }

   switch (u8_timer_type)
   {
      case 8:
         if(u8_timer_nb==2)
         {
            TCCR2B = (TCCR2B & 0xF8) | u8_div_factor_code;
         }
         else
         {
            TCCR0B = (TCCR0B & 0xF8) | u8_div_factor_code;
         }
         break;
      case 16:
         TCCR1B = (TCCR1B & 0xF8) | u8_div_factor_code;
         break;
      default:
         TCCR0B = (TCCR0B & 0xF8) | u8_div_factor_code;
         break;
   }
}


/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_timer_set_val                                          */
/* Description: set initial value of a certain timer                          */
/* Input(s)   : Input  1: uint8 - Counter type (8,16 bits)                    */
/*            : Input  2: uint8 - Counter number                              */
/*            : Input  3: uint16 - Initial Value                              */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_timer_set_val(uint8 u8_timer_type,
                          uint8 u8_timer_nb,
                          uint16 u16_value)
{
   switch (u8_timer_type)
   {
      case 8:
         if(u8_timer_nb==2)
         {
            TCNT2 = (uint8) u16_value;
         }
         else
         {
            TCNT0 = (uint8) u16_value;
         }
         break;
      case 16:
         TCNT1H = (uint8)(u16_value >> 8);
         TCNT1L = (uint8) u16_value;
         break;
      default:
         TCNT0 = (uint8) u16_value;
         break;
   }
}

/*----------------------------------------------------------------------------*/
/*---------------------------- FUNCTION HEADER -------------------------------*/
/* Name       : vd_bsc_timer_int_ovf_enable                                   */
/* Description: Enable overflow interrupt of certain timer                    */
/* Input(s)   : Input  1: uint8 - Counter type (8,16 bits)                    */
/*            : Input  2: uint8 - Counter number                              */
/* Output(s)  : Output 1: void                                                */
/* Author     : Karim Mansour                                                 */
/*----------------------------------------------------------------------------*/
void vd_bsc_timer_int_ovf_enable(uint8 u8_timer_type,
                                 uint8 u8_timer_nb)
{
   switch (u8_timer_type)
   {
      case 8:
         if(u8_timer_nb==2)
         {
            TIMSK2 = _BV(TOIE2);
         }
         else
         {
            TIMSK0 = _BV(TOIE0);
         }
         break;
      case 16:
         TIMSK1 = _BV(TOIE1);
         break;
      default:
         TIMSK0 = _BV(TOIE0);
         break;
   }
}

/*-----------------------------End Of File------------------------------------*/
